//
//  CollectionViewCell.m
//  HimalayaMusic
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 WXCLegendary. All rights reserved.
//

#import "CollectionViewCell.h"

@implementation CollectionViewCell 

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self getData];
        [self creatCollectionView];
    }
    return self;
}

- (void)creatCollectionView {
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.headerReferenceSize = CGSizeMake(self.width, 30);
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.itemSize = CGSizeMake((self.width - 5) / 3, 140);
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.backgroundColor = [UIColor whiteColor];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:self.flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = YES;
    [self.collectionView registerClass:[AnchorCollectionViewCell class] forCellWithReuseIdentifier:@"AnchorCollectionViewCell"];
    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    
    [self.collectionView registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionReusableView"];
    [self addSubview:self.collectionView];
}

- (void)getData {
    
    
    [SANetWorkingTask request:@"http://mobile.ximalaya.com/m/explore_user_index?device=android&page=0" parmater:nil block:^(id result) {
        NSArray *array = result[@"list"];
        NSMutableArray *titleArr = [NSMutableArray arrayWithCapacity:0];
        NSMutableArray *modelArr = [NSMutableArray arrayWithCapacity:0];
        NSMutableArray *nameArr = [NSMutableArray arrayWithCapacity:0];
        NSMutableArray *idArr = [NSMutableArray arrayWithCapacity:0];
        
        for (NSDictionary *dic in array) {
            NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:0];
            NSString *title = [dic objectForKey:@"title"];
            NSString *name = [dic objectForKey:@"name"];
            NSNumber *ids = [dic objectForKey:@"id"];
            
            [titleArr addObject:title];
            [nameArr addObject:name];
            [idArr addObject:ids];
            
            for (NSDictionary *dictionary in dic[@"list"]) {
                
                AnchorModel *model = [[AnchorModel alloc] initWithDictionary:dictionary];
                [mutableArray addObject:model];
                
            }
            [modelArr addObject:mutableArray];
            
        }
        
        
        
        self.titleArray = [NSArray arrayWithArray:titleArr];
        [titleArr removeAllObjects];
        self.nameArray = [NSArray arrayWithArray:nameArr];
        [nameArr removeAllObjects];
        self.idArray = [NSArray arrayWithArray:idArr];
        [idArr removeAllObjects];
        self.modelArray = [NSArray arrayWithArray:modelArr];
        [modelArr removeAllObjects];
        
        [self.collectionView reloadData];
    }];
    
}

#pragma mark -- collectioViewDataSource

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AnchorCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AnchorCollectionViewCell" forIndexPath:indexPath];
    cell.model = self.modelArray[indexPath.section][indexPath.row];
    return cell;
    
}

-(void)changeAndGet:(NSNumber *)number {
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

#pragma mark -- collectionDelegate 

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (self.titleArray.count) {
        return self.titleArray.count;
    }
    return 0;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    CollectionReusableView *resableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionReusableView" forIndexPath:indexPath];
    
    resableView.delegate = self;
    resableView.name = self.nameArray[indexPath.section];
    resableView.ids = self.idArray[indexPath.section];

    if (self.titleArray.count) {
        resableView.string = self.titleArray[indexPath.section];
    }
    
    resableView.delegate = self;
    
    return resableView;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    AnchorDetailViewController *detail = [[AnchorDetailViewController alloc] init];
    AnchorModel *model = self.modelArray[indexPath.section][indexPath.item];
    detail.url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/homePage?position=1&toUid=%@&device=android&title=%@",model.uid,self.titleArray[indexPath.section]];
    detail.titles = self.titleArray[indexPath.section];
    detail.uid = model.uid;
    
    [self.delegates changeVC:model.uid withUrl:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/homePage?position=1&toUid=%@&device=android&title=%@",model.uid,self.titleArray[indexPath.section]] withTitle:self.titleArray[indexPath.section]];
}


-(void)changeAndGet:(NSString *)string withConditon:(NSNumber *)condition {
    [self.delegate changeAndGet:string withCondition:condition];
}







@end
